/* 
 * File:   FractalView.cpp
 * Author: amouro-m
 * 
 * Created on 18 mai 2010, 09:14
 */

#include "FractalView.h"
#include "FractalPrinterNormal.h"
#include "FractalCompute.hpp"
#include "FiltreCompute.hpp"

#include <QMenuBar>
#include <QMenu>

FractalView::FractalView(unsigned int width, unsigned int height, QWidget* parent):
QWidget(parent),
_width(width),
_height(height),
_area(0),
_fractal_printer(0)
{
    setMinimumSize(width, height);
    setMaximumSize(width, height);
    resize(width, height);

    _area = SDLHelpers::createQImageCompliantSurface(SDL_SWSURFACE, width, height);
    FractalPrinter* fp = new FractalPrinterNormal(_area);
    set_fractal_printer(fp);
}

FractalView::~FractalView()
{
    if(_area)
        delete _area;
}

SDL_Surface*
FractalView::get_area() const
{
    return _area;
}

FractalPrinter*
FractalView::get_fractal_printer() const
{
    return _fractal_printer;
}

void
FractalView::set_fractal_printer(FractalPrinter* fractal_printer)
{
    if(_fractal_printer)
    {
        fractal_printer->set_fractal(_fractal_printer->get_fractal());
        disconnect(_fractal_printer, SIGNAL(updated()));
        delete _fractal_printer;
    }
    _fractal_printer = fractal_printer;
    if(_fractal_printer)
    {
        connect(_fractal_printer, SIGNAL(updated()), this, SLOT(repaint()));
    }
}

void
FractalView::filtre_apply(Filtre& filtre)
{
    if(_area)
        FiltreCompute::ParallelCompute(filtre, _fractal_printer);
}

void
FractalView::set_fractal(Fractal* fractal)
{
    _fractal_printer->set_fractal(fractal);
}

void
FractalView::paintEvent(QPaintEvent* e)
{
    if(_area && _fractal_printer)
    {
        QImage im = SDLHelpers::SDLSurfaceToQImage(_area);

        QPainter p(this);
        p.drawImage(0, 0, im);
    }
}


